home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 003 / _gs / !GS / c / GSPATH < prev    next >
Text File  |  1991-10-26  |  9KB  |  293 lines

  1. /* Copyright (C) 1989 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gspath.c */
  21. /* Path construction routines for Ghostscript library */
  22. #include "math_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"
  26. #include "gxmatrix.h"
  27. #include "gxpath.h"
  28. #include "gzstate.h"
  29.  
  30. /* Conversion parameters */
  31. #define degrees_to_radians (M_PI / 180.0)
  32.  
  33. /* ------ Miscellaneous ------ */
  34.  
  35. int
  36. gs_newpath(gs_state *pgs)
  37. {    gx_path_release(pgs->path);
  38.     gx_path_init(pgs->path, &pgs->memory_procs);
  39.     return 0;
  40. }
  41.  
  42. int
  43. gs_closepath(gs_state *pgs)
  44. {    return gx_path_close_subpath(pgs->path);
  45. }
  46.  
  47. /* ------ Points and lines ------ */
  48.  
  49. int
  50. gs_currentpoint(gs_state *pgs, gs_point *ppt)
  51. {    gs_fixed_point pt;
  52.     int code = gx_path_current_point(pgs->path, &pt);
  53.     if ( code < 0 ) return code;
  54.     return gs_itransform(pgs, fixed2float(pt.x), fixed2float(pt.y), ppt);
  55. }
  56.  
  57. int
  58. gs_moveto(gs_state *pgs, floatp x, floatp y)
  59. {    int code;
  60.     gs_fixed_point pt;
  61.     if ( (code = gs_point_transform2fixed(&pgs->ctm, x, y, &pt)) >= 0 )
  62.         code = gx_path_add_point(pgs->path, pt.x, pt.y);
  63.     return code;
  64. }
  65.  
  66. int
  67. gs_rmoveto(gs_state *pgs, floatp x, floatp y)
  68. {    int code;
  69.     gs_fixed_point dpt;
  70.     if ( (code = gs_distance_transform2fixed(&pgs->ctm, x, y, &dpt)) >= 0 )
  71.         code = gx_path_add_relative_point(pgs->path, dpt.x, dpt.y);
  72.     return code;
  73. }
  74.  
  75. int
  76. gs_lineto(gs_state *pgs, floatp x, floatp y)
  77. {    int code;
  78.     gs_fixed_point pt;
  79.     if ( (code = gs_point_transform2fixed(&pgs->ctm, x, y, &pt)) >= 0 )
  80.         code = gx_path_add_line(pgs->path, pt.x, pt.y);
  81.     return code;
  82. }
  83.  
  84. int
  85. gs_rlineto(gs_state *pgs, floatp x, floatp y)
  86. {    gs_fixed_point cpt, dpt;
  87.     int code = gx_path_current_point(pgs->path, &cpt);
  88.     if ( code < 0 ) return code;
  89.     if ( (code = gs_distance_transform2fixed(&pgs->ctm, x, y, &dpt)) >= 0 )
  90.         code = gx_path_add_line(pgs->path, cpt.x + dpt.x, cpt.y + dpt.y);
  91.     return code;
  92. }
  93.  
  94. /* ------ Arcs ------ */
  95.  
  96. /* Forward declarations */
  97. private int arc_either(P7(gs_state *,
  98.   floatp, floatp, floatp, floatp, floatp, int));
  99. private int arc_add(P8(gs_state *,
  100.   floatp, floatp, floatp, floatp, floatp, floatp, int));
  101.  
  102. int
  103. gs_arc(gs_state *pgs,
  104.   floatp xc, floatp yc, floatp r, floatp ang1, floatp ang2)
  105. {    return arc_either(pgs, xc, yc, r, ang1, ang2, 0);
  106. }
  107.  
  108. int
  109. gs_arcn(gs_state *pgs,
  110.   floatp xc, floatp yc, floatp r, floatp ang1, floatp ang2)
  111. {    return arc_either(pgs, xc, yc, r, ang1, ang2, 1);
  112. }
  113.  
  114. private int
  115. arc_either(gs_state *pgs,
  116.   floatp axc, floatp ayc, floatp ar, floatp aang1, floatp aang2,
  117.   int clockwise)
  118. {    fixed ang1 = float2fixed(aang1), ang2 = float2fixed(aang2), adiff;
  119.     float ang1r;
  120.     float x0, y0, sin0, cos0;
  121.     float x3r, y3r;
  122.     int first = 1;
  123.     int code;
  124.     if ( ar < 0 ) return_error(gs_error_rangecheck);
  125. #define fixed90 int2fixed(90)
  126. #define fixed360 int2fixed(360)
  127.     /* Reduce the arc to at most 360 degrees. */
  128.     if ( ang1 != ang2 )
  129.        {    ang1 %= fixed360;
  130.         ang2 %= fixed360;
  131.         if ( clockwise )
  132.            {    if ( ang2 >= ang1 ) ang1 += fixed360;
  133.            }
  134.         else
  135.            {    if ( ang2 <= ang1 ) ang2 += fixed360;
  136.            }
  137.        }
  138.     ang1r = fixed2float(ang1) * degrees_to_radians;
  139.     sin0 = ar * sin(ang1r), cos0 = ar * cos(ang1r);
  140.     x0 = axc + cos0, y0 = ayc + sin0;
  141.     if ( clockwise )
  142.        {    /* Quadrant reduction */
  143.         while ( (adiff = ang2 - ang1) < -fixed90 )
  144.            {    float w = sin0; sin0 = -cos0; cos0 = w;
  145.             x3r = axc + cos0, y3r = ayc + sin0;
  146.             /* Must cast doubles to floats explicitly */
  147.             code = arc_add(pgs, x0, y0, x3r, y3r,
  148.                 (x0 + cos0),
  149.                 (y0 + sin0),
  150.                 first);
  151.             if ( code < 0 ) return code;
  152.             x0 = x3r, y0 = y3r;
  153.             ang1 -= fixed90;
  154.             first = 0;
  155.            }
  156.        }
  157.     else
  158.        {    /* Quadrant reduction */
  159.         while ( (adiff = ang2 - ang1) > fixed90 )
  160.            {    float w = cos0; cos0 = -sin0; sin0 = w;
  161.             x3r = axc + cos0, y3r = ayc + sin0;
  162.             /* Must cast doubles to floats explicitly */
  163.             code = arc_add(pgs, x0, y0, x3r, y3r,
  164.                 (x0 + cos0),
  165.                 (y0 + sin0),
  166.                 first);
  167.             if ( code < 0 ) return code;
  168.             x0 = x3r, y0 = y3r;
  169.             ang1 += fixed90;
  170.             first = 0;
  171.            }
  172.        }
  173.     /* Compute the intersection of the tangents. */
  174.        {    float trad = tan(fixed2float(adiff) * (degrees_to_radians / 2));
  175.         float ang2r = fixed2float(ang2) * degrees_to_radians;
  176.         code = arc_add(pgs, x0, y0,
  177.             (axc + ar * cos(ang2r)),
  178.             (ayc + ar * sin(ang2r)),
  179.             (x0 - trad * sin0),
  180.             (y0 + trad * cos0),
  181.             first);
  182.        }
  183.     return code;
  184. }
  185.  
  186. int
  187. gs_arcto(gs_state *pgs,
  188.   floatp ax1, floatp ay1, floatp ax2, floatp ay2, floatp arad,
  189.   float *retxy)            /* float retxy[4] */
  190. {    float xt0, yt0, xt2, yt2;
  191.     gs_point up0;
  192. #define ax0 up0.x
  193. #define ay0 up0.y
  194.     int code;
  195.     if ( arad < 0 )
  196.         return_error(gs_error_undefinedresult);
  197.     /* Transform the current point back into user coordinates */
  198.     if ( (code = gs_currentpoint(pgs, &up0)) < 0 ) return code;
  199.        {    /* Now we have to compute the tangent points. */
  200.         /* Basically, the idea is to compute the tangent */
  201.         /* of the bisector by using tan(x+y) and tan(z/2) */
  202.         /* formulas, without ever using any trig. */
  203.         float dx0 = ax0 - ax1, dy0 = ay0 - ay1;
  204.         float dx2 = ax2 - ax1, dy2 = ay2 - ay1;
  205.         /* Compute the squared lengths from p1 to p0 and p2. */
  206.         double sql0 = dx0 * dx0 + dy0 * dy0;
  207.         double sql2 = dx2 * dx2 + dy2 * dy2;
  208.         /* Compute the distance from p1 to the tangent points. */
  209.         /* This is the only hairy part. */
  210.         double num = dy0 * dx2 - dy2 * dx0;
  211.         double denom = sqrt(sql0 * sql2) - (dx0 * dx2 + dy0 * dy2);
  212.         /* Check for collinear points. */
  213.         if ( fabs(num) < 1.0e-6 || fabs(denom) < 1.0e-6)
  214.            {    gs_fixed_point pt;
  215.             code = gs_point_transform2fixed(&pgs->ctm, ax1, ay1, &pt);
  216.             if ( code >= 0 ) code = gx_path_add_line(pgs->path, pt.x, pt.y);
  217.             xt0 = xt2 = ax1;
  218.             yt0 = yt2 = ay1;
  219.            }
  220.         else        /* not collinear */
  221.            {    double dist = fabs(arad * num / denom);
  222.             double l0 = dist / sqrt(sql0), l2 = dist / sqrt(sql2);
  223.             xt0 = ax1 + dx0 * l0;
  224.             yt0 = ay1 + dy0 * l0;
  225.             xt2 = ax1 + dx2 * l2;
  226.             yt2 = ay1 + dy2 * l2;
  227.             code = arc_add(pgs, xt0, yt0, xt2, yt2, ax1, ay1, 1);
  228.            }
  229.        }
  230.     if ( retxy != 0 )
  231.        {    retxy[0] = xt0;
  232.         retxy[1] = yt0;
  233.         retxy[2] = xt2;
  234.         retxy[3] = yt2;
  235.        }
  236.     return code;
  237. }
  238.  
  239. /* Internal routine for adding an arc to the path. */
  240. private int
  241. arc_add(gs_state *pgs,
  242.   floatp x0, floatp y0, floatp x3, floatp y3, floatp xt, floatp yt,
  243.   int first)
  244. {    gx_path *path = pgs->path;
  245.     int code;
  246.     gs_fixed_point p0, p3, pt, cpt;
  247. #ifdef DEBUG
  248. if ( gs_debug['r'] )
  249.     dprintf7("[r]Arc p0=(%f,%f) pt=(%f,%f) p3=(%f,%f) first=%d\n",
  250.          x0, y0, xt, yt, x3, y3, first);
  251. #endif
  252.     if (    (code = gs_point_transform2fixed(&pgs->ctm, x0, y0, &p0)) < 0 ||
  253.         (code = gs_point_transform2fixed(&pgs->ctm, x3, y3, &p3)) < 0 ||
  254.         (code = gs_point_transform2fixed(&pgs->ctm, xt, yt, &pt)) < 0 ||
  255.         (first && (code = (gx_path_current_point(path, &cpt) >= 0 ?
  256.              gx_path_add_line(path, p0.x, p0.y) :
  257.              gx_path_add_point(path, p0.x, p0.y))) < 0)
  258.        )
  259.         return code;
  260.     return gx_path_add_arc(path, p0.x, p0.y, p3.x, p3.y, pt.x, pt.y);
  261. }
  262.  
  263. /* ------ Curves ------ */
  264.  
  265. int
  266. gs_curveto(gs_state *pgs,
  267.   floatp x1, floatp y1, floatp x2, floatp y2, floatp x3, floatp y3)
  268. {    gs_fixed_point p1, p2, p3;
  269.     int code;
  270.     if (    (code = gs_point_transform2fixed(&pgs->ctm, x1, y1, &p1)) < 0 ||
  271.         (code = gs_point_transform2fixed(&pgs->ctm, x2, y2, &p2)) < 0 ||
  272.         (code = gs_point_transform2fixed(&pgs->ctm, x3, y3, &p3)) < 0
  273.        ) return code;
  274.     return gx_path_add_curve(pgs->path,
  275.         p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
  276. }
  277.  
  278. int
  279. gs_rcurveto(gs_state *pgs,
  280.   floatp dx1, floatp dy1, floatp dx2, floatp dy2, floatp dx3, floatp dy3)
  281. {    gs_fixed_point pt, p1, p2, p3;
  282.     int code = gx_path_current_point(pgs->path, &pt);
  283.     if ( code < 0 ) return code;
  284.     if (    (code = gs_distance_transform2fixed(&pgs->ctm, dx1, dy1, &p1)) < 0 ||
  285.         (code = gs_distance_transform2fixed(&pgs->ctm, dx2, dy2, &p2)) < 0 ||
  286.         (code = gs_distance_transform2fixed(&pgs->ctm, dx3, dy3, &p3)) < 0
  287.        ) return code;
  288.     return gx_path_add_curve(pgs->path,
  289.         pt.x + p1.x, pt.y + p1.y,
  290.         pt.x + p2.x, pt.y + p2.y,
  291.         pt.x + p3.x, pt.y + p3.y);
  292. }
  293.